package com.longhu.service.impl;

import com.longhu.exception.BaseException;
import com.longhu.exception.BizCodeEnum;
import com.longhu.util.ComputeTotalPriceUtil;
import com.longhu.service.ComputerPriceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;

@Service("computerPriceService")
public class ComputerPriceServiceImpl implements ComputerPriceService, InitializingBean {
    private static final Logger log = LoggerFactory.getLogger(ComputerPriceServiceImpl.class);
    @Value("${unitPrice.appUnitPrice}")
    private String appUnitPrice;

    @Value("${unitPrice.strawberryUnitPrice}")
    private String strawberryUnitPrice;

    @Value("${unitPrice.mongoUnitPrice}")
    private String mongoUnitPrice;


    @Override
    public void afterPropertiesSet() {
        if (appUnitPrice == null || "".equals(appUnitPrice)) {
            log.error("苹果单价为空，请检查配置！");
            throw new BaseException(BizCodeEnum.APPLE_UNIT_PRICE_NOT_EXIST.getCode()
                    , BizCodeEnum.APPLE_UNIT_PRICE_NOT_EXIST.getMsg());
        }

        if (strawberryUnitPrice == null || "".equals(strawberryUnitPrice)) {
            log.error("草莓单价为空，请检查配置！");
            throw new BaseException(BizCodeEnum.STRAWBERRY_UNIT_PRICE_NOT_EXIST.getCode()
                    , BizCodeEnum.STRAWBERRY_UNIT_PRICE_NOT_EXIST.getMsg());
        }

        if (mongoUnitPrice == null || "".equals(mongoUnitPrice)) {
            log.error("芒果单价为空，请检查配置！");
            throw new BaseException(BizCodeEnum.MONGO_UNIT_PRICE_NOT_EXIST.getCode()
                    , BizCodeEnum.MONGO_UNIT_PRICE_NOT_EXIST.getMsg());
        }
    }

    /**
     * 计算顾客买的苹果和草莓的总价
     *
     * @param appleCount      苹果斤数
     * @param strawberryCount 草莓斤数
     * @return
     */
    @Override
    public BigDecimal questionA(BigDecimal appleCount, BigDecimal strawberryCount) {

        BigDecimal totalPrice = ComputeTotalPriceUtil.computeTotalPrice(
                Optional.ofNullable(appleCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(appUnitPrice))
                , strawberryCount.multiply(new BigDecimal(strawberryUnitPrice)));
        return totalPrice;
    }

    /**
     * 计算顾客买的苹果、草莓和芒果的总价
     *
     * @param appleCount      苹果斤数
     * @param strawberryCount 草莓斤数
     * @param mangoCount      芒果斤数
     * @return
     */
    @Override
    public BigDecimal questionB(BigDecimal appleCount, BigDecimal strawberryCount, BigDecimal mangoCount) {
        BigDecimal totalPrice = ComputeTotalPriceUtil.computeTotalPrice(Optional.ofNullable(appleCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(appUnitPrice))
                , Optional.ofNullable(strawberryCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(strawberryUnitPrice))
                , Optional.ofNullable(mangoCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(mongoUnitPrice)));
        return totalPrice;
    }

    /**
     * 计算顾客买的苹果、草莓和芒果的总价在草莓打折后
     *
     * @param strawberryDiscount 草莓折扣
     * @param appleCount         苹果斤数
     * @param strawberryCount    草莓斤数
     * @param mangoCount         芒果斤数
     * @return
     */
    @Override
    public BigDecimal questionC(BigDecimal strawberryDiscount, BigDecimal appleCount, BigDecimal strawberryCount, BigDecimal mangoCount) {
        BigDecimal totalPrice = ComputeTotalPriceUtil.computeTotalPrice(Optional.ofNullable(appleCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(appUnitPrice))
                , Optional.ofNullable(strawberryCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(strawberryUnitPrice).multiply(Optional.ofNullable(strawberryDiscount).orElse(BigDecimal.ZERO)))
                , Optional.ofNullable(mangoCount).orElse(BigDecimal.ZERO).multiply(new BigDecimal(mongoUnitPrice)));
        return totalPrice;
    }

    /**
     * 计算顾客买的苹果、草莓和芒果的总价在草莓打折并且满减活动后
     *
     * @param strawberryDiscount 草莓折扣
     * @param appleCount         苹果斤数
     * @param strawberryCount    草莓斤数
     * @param mangoCount         芒果斤数
     * @param fullPrice          满多少
     * @param reducePrice        减多少
     * @return
     */
    @Override
    public BigDecimal questionD(BigDecimal strawberryDiscount, BigDecimal appleCount, BigDecimal strawberryCount,
                                BigDecimal mangoCount, BigDecimal fullPrice, BigDecimal reducePrice) {
        BigDecimal totalPrice = questionC(strawberryDiscount, appleCount, strawberryCount, mangoCount);

        if (fullPrice == null) {
            log.error("满多少值为空！");
            throw new BaseException(BizCodeEnum.FULL_PRICE_NOT_EXIST.getCode(), BizCodeEnum.FULL_PRICE_NOT_EXIST.getMsg());
        }

        if (reducePrice == null) {
            log.error("减多少值为空！");
            throw new BaseException(BizCodeEnum.REDUCE_PRICE_NOT_EXIST.getCode(), BizCodeEnum.REDUCE_PRICE_NOT_EXIST.getMsg());

        }

        // 满减活动
        if (totalPrice != null) {
            totalPrice = totalPrice.compareTo(fullPrice) != -1
                    ? totalPrice.subtract(reducePrice)
                    : totalPrice;
        }

        return totalPrice;
    }


}
